package control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import model.Shetuan;
import util.Pagination;
import db.DBConnect;

public class ShetuanControl {
	public static Pagination pageInstance = null;
	
	/**
	 * 增加
	 * @author dujuan
	 * @return
	 * 2012-02-23
	 */
	public boolean insertShetuan(Shetuan shetuan)
	{
		DBConnect dbc = new DBConnect();
		String sql ="insert into shetuan (shetuanName,password) values (?,?)";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, shetuan.getShetuanName());
			dbc.setString(2, shetuan.getPassword());
					
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally {
			dbc.close();
		}
	}
	
	/*判断数据库里面是否存在用户*/
	public static boolean isShetuan(String a,String b)
	{
		DBConnect dbc = new DBConnect();
		String sql ="select * from shetuan where shetuanname = ? and password=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,a);
			dbc.setString(2,b);
			ResultSet rs= dbc.executeQuery();
			if(rs.next()){
				return true;
			}
			else{
				return false;
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			dbc.close();
		}return false;
	}
	
	
	/**
	 * 根据id获取信息
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public Shetuan GetShetuan(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from shetuan where shetuanId = ?";

		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			ResultSet rs = dbc.executeQuery();
			Shetuan shetuan = new Shetuan();
			
			if(rs.next())
			{
				shetuan.setShetuanName(rs.getString("shetuanName"));
				shetuan.setPassword(rs.getString("password"));
				
				return shetuan;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		return null;
	}
	
	/*
	 * 根据名字获取Id
	 * */
	public int getShetuanId(String shetuanName){
		DBConnect dbc = new DBConnect();
		String sql = "SELECT * FROM shetuan WHERE shetuanName = ?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1,shetuanName);
			ResultSet rs = dbc.executeQuery();
			if(rs.next())
			return rs.getInt("id");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		} finally {
			dbc.close();
		}
		return 0;
	}
	
	
	/**
	 * 删除
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean deleteShetuan(int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "delete from shetuan where shetuanId =?";
		
		dbc.prepareStatement(sql);
		
		try {
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}	
	}
	
	/**
	 * 修改
	 * @author dujuan
	 * 2012-02-23
	 */
	
	public boolean updateShetuan (Shetuan shetuan, int id)
	{
		DBConnect dbc = new DBConnect();
		String sql = "update shetuan set shetuanName=? ,password=? where shetuanId =?";
		dbc.prepareStatement(sql);
		
		try {
			dbc.setString(1, shetuan.getShetuanName());
			dbc.setString(2, shetuan.getPassword());
			dbc.setInt(3, id);
			
			//System.out.println("~~~: " + id);
			dbc.executeUpdate();
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	
	
	/**
	 * 列表返回显示总结
	 * 
	 */
	
	public Collection GetShetuan()
	{
		DBConnect dbc = new DBConnect();
		String sql = "select * from shetuan order by shetuanId desc";
		dbc.prepareStatement(sql);
		Collection<Shetuan> shetuanList = new ArrayList<Shetuan>();
		try {
			ResultSet rs = dbc.executeQuery();
			while(rs.next())
			{
				
				Shetuan shetuan = new Shetuan();
				shetuan.setShetuanId(rs.getInt("shetuanId"));
				shetuan.setShetuanName(rs.getString("shetuanName"));
				shetuan.setPassword(rs.getString("password"));
			}
			return shetuanList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbc.close();
		}
		
		return null;
	}
	


	/*
	 * 返回总结列表，分页显示
	 */
	
	public Collection GetShetuan(int page, int rowsPerPage)
	{
		String sql = "select * from shetuan order by shetuanId desc";
		pageInstance = new ShetuanControlPage(sql,rowsPerPage);
		
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;

	}
	

class ShetuanControlPage extends Pagination{
	public ShetuanControlPage(String sql, int rowsPerPage)
	{
		super(sql,rowsPerPage,1);
	}
	
	@Override
	protected Collection<Shetuan> packResultSet(ResultSet rs) throws SQLException {
		// TODO Auto-generated method stub
		Collection<Shetuan> shetuanList = new ArrayList<Shetuan>();
		while(rs.next())
		{
			Shetuan shetuan = new Shetuan();
			
			shetuan.setShetuanId(rs.getInt("shetuanId"));	
			shetuan.setShetuanName(rs.getString("shetuanName"));
			shetuan.setPassword(rs.getString("password"));
			
				
			shetuanList.add(shetuan);
			shetuan = null;
			}
			return shetuanList;
			
		}
	
	}
	
}
	


	
	
	

	

	
	
